Skip to content

datetime: new format "p", same as "P" but returning "Z" for UTC #5896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

gharlan
Copy link
Contributor

@gharlan gharlan commented Jul 26, 2020

ISO 8601 differentiates between local time (without time zone), UTC with letter "Z" and time offsets from UTC like "+02:00".
https://en.wikipedia.org/wiki/ISO_8601#Time_zone_designators

When formatting datetimes, it is sometimes desired to get the "Z" for UTC times and otherwise the offset.
But at the moment there are only the formatting chars "O" (for "+0200" etc.) and "P" (for "+02:00" etc.).

I suggest to add another formatting char "Q" lower case "p" which behaves the same as "P" but returns "Z" for UTC datetimes.

$datetime = new DateTime('2020-07-26 20:00:00', new DateTimeZone('UTC'));
$datetime->format('Y-m-d\TH:i:sP'); // '2020-07-26T20:00:00+00:00'
$datetime->format('Y-m-d\TH:i:sp'); // '2020-07-26T20:00:00Z'

$datetime = new DateTime('2020-07-26 20:00:00', new DateTimeZone('Europe/London'));
$datetime->format('Y-m-d\TH:i:sP'); // '2020-07-26T20:00:00+00:00'
$datetime->format('Y-m-d\TH:i:sp'); // '2020-07-26T20:00:00+00:00'

$datetime = new DateTime('2020-07-26 20:00:00', new DateTimeZone('Europe/Berlin'));
$datetime->format('Y-m-d\TH:i:sP'); // '2020-07-26T20:00:00+02:00'
$datetime->format('Y-m-d\TH:i:sp'); // '2020-07-26T20:00:00+02:00'

I'm not sure if it is useful enough to deserve a new formatting char. So don't worry about rejecting this proposal!

@nikic nikic requested a review from derickr July 27, 2020 07:52
@gharlan gharlan force-pushed the datetime-format-utc branch from ea9ad8e to 60e381b Compare July 27, 2020 11:42
@gharlan gharlan changed the title datetime: new format "Q", same as "P" but returning "Z" for UTC datetime: new format "p", same as "P" but returning "Z" for UTC Jul 27, 2020
Copy link
Member

@derickr derickr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Can you make a ticket at bugs.php.net, so that we can add a line to the NEWS file too? Feel free to include that in this PR. The formatting of the NEWS file is finicky :-)

@gharlan gharlan force-pushed the datetime-format-utc branch from 60e381b to e9551a5 Compare July 27, 2020 17:02
@gharlan
Copy link
Contributor Author

gharlan commented Jul 27, 2020

Can you make a ticket at bugs.php.net, so that we can add a line to the NEWS file too? Feel free to include that in this PR. The formatting of the NEWS file is finicky :-)

Done. I hope everything is correct.

@cmb69
Copy link
Member

cmb69 commented Jul 28, 2020

[…], so that we can add a line to the NEWS file too? Feel free to include that in this PR.

I'd suggest not to add NEWS entries to PRs, because that can result in merge conflicts pretty quickly.

Anyhow, the merger should also add a note to UPGRADING, so that this feature addition will not be forgotten for the migration guide in the PHP manual. Thanks.

@cmb69
Copy link
Member

cmb69 commented Aug 3, 2020

Applied as a6e3ce4.

@cmb69 cmb69 closed this Aug 3, 2020
@nikic nikic added this to the PHP 8.0 milestone Aug 3, 2020
@gharlan gharlan deleted the datetime-format-utc branch August 3, 2020 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants